// pages/order/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    password: "",
    userInfo: {
      isGet: false,
      _id: "",
      awardList: [{
        name: ""
      }]
    },
  },
  onReady() {
    this.setData({
      password: wx.getStorageSync("password") || ''
    })
  },
  err(result: any) {
    wx.showModal({
      title: '兑奖失败',
      content: `用户已经兑奖了${result.data.awardList[0].name}`,
      showCancel: false,
      confirmText: "关闭"
    })
  },
  up(res: {
    _id: string
  }) {
    const { password } = this.data;
    wx.showLoading({
      title: ""
    })
    wx.cloud.callFunction({
      name: 'activelog',
      data: { action: 'up', params: { _id: res._id, password} },
      success({result}: any) {
        if (result.success) {
          wx.showToast({
            icon: "none",
            title: "兑奖成功",
          })
          wx.setStorageSync("password", password)
        } else {
          wx.showToast({
            icon: "error",
            title: result.errMsg,
          })
        }
        wx.hideLoading()
       
      }
    })
  },
  bindinputPassword(e: any) {
    this.setData({
      password: e.detail.value
    })
  },
  info(result: any){
    const that = this;
    wx.showModal({
      title: '是否兑奖',
      content: `${result.data.awardList[0].name}`,
      confirmText: "兑换",
      success (res) {
        if (res.confirm) {
          that.up(result.data)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  details(_id: string) {
    const that = this
    wx.showLoading({
      title: ""
    })
    wx.cloud.callFunction({
      name: 'activelog',
      data: { action: 'details', params: { _id } },
      success({result}: any) {
        console.log(result);
        if (result.success) {
          if(result.data.isGet) {
            that.err(result)
          } else {
            that.info(result)
          }
        } else {
          wx.showToast({
            icon: "error",
            title: "兑奖失败",
          })
        }
        wx.hideLoading()
      }
    })
  },
  scanCode() {
    const that = this
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ['qrCode'], // 只扫描二维码
      success(res: {
        result: string
      }) {
        that.details(res.result)
        // 在这里处理扫描到的二维码信息
      },
      fail(err) {
        this
        console.error('扫描失败:', err);
      }
    });
  }
 
})